//
//  CNPlayerLevelViewController.m
//  chuiniu
//
//  Created by zheng ji on 14-10-10.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "CNPlayerLevelViewController.h"
#import "CNGoldCoin.h"
#import "CNPlayer.h"
#import "CommandDef.h"
#import "CNAppDelegate.h"
#import "CNGameUtil.h"
#import "CNViewController.h"
@interface CNPlayerLevelViewController ()
{
    NSArray* _goldCoinArray;
    CNGameClient* _gameClient;
    CNAppDelegate* _appDelegate;
    iToast *toast;
    CNPlayer* _currentPlayer;
    NSUserDefaults* _userDefaults;
    CNPlayer* tempVSPlayer;
}
@end

@implementation CNPlayerLevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
  /*  _goldCoinArray=[NSArray arrayWithObjects:[CNGoldCoin initWith:@"屌丝" coins:@"1000"],[CNGoldCoin initWith:@"屌丝" coins:@"1000"]
                    ,[CNGoldCoin initWith:@"短工" coins:@"1500"],[CNGoldCoin initWith:@"长工" coins:@"2500"],[CNGoldCoin initWith:@"佃户" coins:@"4000"],[CNGoldCoin initWith:@"贫农" coins:@"8000"],[CNGoldCoin initWith:@"渔夫" coins:@"14000"],[CNGoldCoin initWith:@"猎人" coins:@"23000"],[CNGoldCoin initWith:@"中农" coins:@"36500"],[CNGoldCoin initWith:@"富农" coins:@"50000"],[CNGoldCoin initWith:@"掌柜" coins:@"70000"],[CNGoldCoin initWith:@"商人" coins:@"100000"],[CNGoldCoin initWith:@"衙役" coins:@"150000"],[CNGoldCoin initWith:@"小财主" coins:@"220000"],[CNGoldCoin initWith:@"大财主" coins:@"300000"],[CNGoldCoin initWith:@"小地主" coins:@"400000"],[CNGoldCoin initWith:@"大地主" coins:@"550000"],[CNGoldCoin initWith:@"知县" coins:@"770000"],[CNGoldCoin initWith:@"通判" coins:@"1000000"],[CNGoldCoin initWith:@"知府" coins:@"1400000"],[CNGoldCoin initWith:@"总督" coins:@"2000000"],[CNGoldCoin initWith:@"巡抚" coins:@"3000000"],[CNGoldCoin initWith:@"丞相" coins:@"4500000"],[CNGoldCoin initWith:@"帝王" coins:@"7000000"], nil];
   */
    _goldCoinArray=[CNGameUtil getAllPlayerLevel];
    
    _appDelegate=(CNAppDelegate*)[UIApplication sharedApplication].delegate;
    _userDefaults=[NSUserDefaults standardUserDefaults];
    
    
    
    
    _appDelegate=(CNAppDelegate*)[UIApplication sharedApplication].delegate;
    _gameClient=_appDelegate.gameClient;
    _gameClient.gameMainViewController=self;
    
    _currentPlayer=[CNGameUtil getCurrentPlayerFromUserDefaults];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return _goldCoinArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReuseCell" forIndexPath:indexPath];
    
    CNGoldCoin* gc=(CNGoldCoin*)_goldCoinArray[indexPath.row];
    cell.textLabel.text=gc.levelName;
    cell.detailTextLabel.text=gc.coins;
    // Configure the cell...
    
    return cell;
}
-(void)toastTimeOver:(int)toastTag{
    
}

/**
 有玩家邀请对战
 */
-(void) requestPK:(CNPlayer*)vsPlayer{
    
    tempVSPlayer=vsPlayer;
    UIAlertView* alertView=[[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"玩家[%@]想和您对战，是否接受?",vsPlayer.playerName] delegate: self cancelButtonTitle:@"接受" otherButtonTitles:@"残忍拒绝", nil];
    // alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
    alertView.tag=tagAlertViewRequestPK;
    [alertView show];
    alertView=nil;
    
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if([alertView tag]==tagAlertViewRequestPK){
        if(  buttonIndex == 0) {
            
            //接受请求对战
            [self startMutilPlay];
        }else{
            //拒绝对战请求
            [_appDelegate.gameClient sendRejectVSCommand:tempVSPlayer];
        }
    }
}
//开启对战游戏
-(void)startMutilPlay{
    
    
    //发送接收对战消息
    
    [_appDelegate.gameClient sendAcceptVSCommand:_currentPlayer vsPlayerID:tempVSPlayer];
    
    CNViewController* viewCtrl=(CNViewController*)[ self.storyboard instantiateViewControllerWithIdentifier:@"CNViewController"];
    
    viewCtrl.gameModel=GameModelMutilPlayer;
    //viewCtrl.gameStatus=GameStatusNormal;
    _appDelegate.vsPlayer=tempVSPlayer;
    _appDelegate.gamePlayModel=GameModelMutilPlayer;
    _appDelegate.gameClient.gameMainViewController=viewCtrl;
    _appDelegate.gameStatus=GameStatusPlaying;
    [self presentViewController:viewCtrl animated:YES completion:nil];
}

@end
